package com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.bean.XtAirlinesPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.service.XtAirlinesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/pages/basic_data_manage/xt_airlines")
public class XtAirlinesController extends BaseController {

    @Autowired
    private XtAirlinesService xtAirlinesService;

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/basic_data_manage/xt_airlines/xtairlines";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) throws Exception {
        if(param == null || param.isEmpty()){
            throwException("查询参数错误");
        }
        return xtAirlinesService.getPageList(param);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true) XtAirlinesPojo pojo) throws Exception {
        if(pojo == null){
            throwException("保存航空公司参数错误");
        }

        pojo.setId(this.getSysStrId());
        int affectRows = xtAirlinesService.insertInit(pojo);

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("保存航空公司成功!");
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("保存航空公司失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody(required = true)XtAirlinesPojo pojo) throws Exception {
        if(pojo == null){
            throwException("更新航空公司参数错误");
        }
        int affectRows = xtAirlinesService.updateInit(pojo);
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("更新航空公司成功!");
        if (affectRows <= 0) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("更新航空公司失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) Object id) throws Exception {
        if(DataTypeUtil.isEmptyStr(id)){
            throwException("删除航空公司参数错误");
        }
        int affectRows = xtAirlinesService.deleteInit(DataTypeUtil.toNotNullString(id));
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("删除航空公司成功!");
        if (affectRows <= 0) {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("删除航空公司失败!");
        }
        return baseResultMap;
    }


}
